package net.caidingke.shepherd.controller;

import io.swagger.annotations.ApiOperation;
import net.caidingke.shepherd.controller.base.BasicController;
import net.caidingke.shepherd.domain.user.Permission;
import net.caidingke.shepherd.domain.user.request.PermissionRequest;
import net.caidingke.shepherd.exception.NotFoundException;
import net.caidingke.shepherd.service.UserService;
import net.caidingke.shepherd.utils.constant.GlobalConfig;
import net.caidingke.shepherd.utils.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author bowen.
 */
@RestController
@RequestMapping(value = GlobalConfig.API_PRE_FIX)
public class UserPermissionController extends BasicController {

    private final UserService userService;

    @Autowired
    public UserPermissionController(UserService userService) {
        this.userService = userService;
    }

    @ApiOperation(value = "获取权限列表")
    @GetMapping(value = "/permission")
    public Result<List<Permission>> permissions() {
        return ok(userService.findPermissions());
    }

    @ApiOperation(value = "新建权限")
    @PostMapping(value = "/permission")
    public Result permission(@RequestBody PermissionRequest request) {
        userService.permission(request);
        return ok();
    }

    @ApiOperation(value = "根据ID获取权限")
    @GetMapping(value = "/permission/{id}")
    public Result<Permission> getUserPermission(@PathVariable(value = "id") Long id) {
        return ok(userService.findPermissionById(id).orElseThrow(NotFoundException::new));
    }

    @ApiOperation(value = "修改权限信息")
    @PutMapping(value = "/permission/{id}")
    public Result modifyPermission(
            @PathVariable(value = "id") Long id, @RequestBody PermissionRequest request) {
        userService.modifyPermission(id, request);
        return ok();
    }
}
